<template>
  <div id="detail">
    <detail-nav-bar
      class="detail-nav"
      @titleClick="titleClick"
      ref="nav"
    ></detail-nav-bar>
    <refresh v-show="isShow"></refresh>

    <scroll
      class="scroll"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
    >
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></detail-goods-info>
      <detail-param-info :paramInfo="paramInfo" ref="param"></detail-param-info>
      <detail-comment-info
        :comment-info="commentInfo"
        ref="comment"
      ></detail-comment-info>

      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>

    <back-top @backClick="backClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar
      @addToCart="addToCart"
      @buyClick="buyClick"
    ></detail-bottom-bar>
    <detail-buy
      v-if="isBuyShow"
      :buy-or-collect="buyOrCollect"
      :buy-price="buyPrice"
    ></detail-buy>
  </div>
</template>

<script>
// 导航栏
import DetailNavBar from "./childComps/DetailNavBar.vue";
// 轮播图
import DetailSwiper from "./childComps/DetailSwiper";
// 商品信息
import DetailBaseInfo from "./childComps/DetailBaseInfo";
// 店铺信息
import DetailShopInfo from "./childComps/DetailShopInfo";
// 商品详细信息
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
// 参数信息
import DetailParamInfo from "./childComps/DetailParamInfo";
// 评论信息
import DetailCommentInfo from "./childComps/DetailCommentInfo";
// 商品推荐
import GoodsList from "components/content/goods/GoodsList.vue";
// 购物栏
import DetailBottomBar from "./childComps/DetailBottomBar";
// 购买弹出
import DetailBuy from "./childComps/DetailBuy";

// 滚动封装
import Scroll from "components/common/scroll/Scroll.vue";
// 回顶按钮
import { itemListenermixin, backTopMixin } from "common/mixin.js";

// 防抖
import { debounce } from "common/utils";
// 请求数据与数据规格
import {
  getDetail,
  getRecommend,
  Goods,
  Shop,
  GoodsParam,
} from "network/detail.js";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    DetailBuy,
    GoodsList,
    Scroll,
  },
  mixins: [backTopMixin, itemListenermixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      isBuyShow: false,
      buyOrCollect: "",
      buyPrice: "",
    };
  },
  created() {
    this.iid = this.$route.params.iid;

    // 页面信息
    this._getDetail(this.iid);
    // 商品推荐(商品推荐图片与主页图片不一样)
    this._getRecommend();

    // 导航点击获取位置解决方法
    this.getThemeTopY = debounce(() => {
      // 每次进来时设置为空
      this.themeTopYs = [];

      // 商品
      this.themeTopYs.push(0);
      // 参数
      this.themeTopYs.push(this.$refs.param.$el.offsetTop);
      // 评论
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      // 推荐
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);

      // 为了让最后的推荐有值比较 需要将比他大的值放入数组中
      this.themeTopYs.push(Number.MAX_VALUE);
    });
  },
  mounted() {
    // 图片渲染完刷新一下
    this.itemListenermixin;
  },

  methods: {
    /*请求数据*/
    // 页面信息
    _getDetail(iid) {
      getDetail(iid).then((res) => {
        console.log(res);

        const data = res.result;

        // (取出轮播图数据)
        this.topImages = data.itemInfo.topImages;

        // (取出商品信息)
        this.goods = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        );

        // (取出店铺信息)
        this.shop = new Shop(data.shopInfo);

        // (取出商品详细信息)
        this.detailInfo = data.detailInfo;

        // (取出参数)
        this.paramInfo = new GoodsParam(
          data.itemParams.info,
          data.itemParams.rule
        );

        // 取出评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0];
        }
      });
    },

    // 商品推荐(商品推荐图片与主页图片不一样)
    _getRecommend() {
      getRecommend().then((res) => {
        // console.log(res);
        this.recommends = res.data.list;
      });
    },

    /*点击事件*/
    // 判断位置相关
    contentScroll(position) {
      // console.log(position);
      // 判断回顶是否显示
      this.listenShowBackTop(position);
      // 下拉刷新
      this.topRefresh(position);

      const positionY = -position.y;
      // 根据位置判断导航
      let length = this.themeTopYs.length;
      // console.log(this.$refs.recommend.$el.offsetTop);
      // 为了让最后的推荐有值比较 需要将比他大的值放入数组中
      for (let i = 0; i < length - 1; i++) {
        // 判断
        // currentIndex ！== 1的时候 就是themeTopYs[i]不是上一个组件的位置时
        // positionY>= this.themeTopYs[i] 就是所处位置比该组件位置大时并
        // positionY < this.themeTopYs[i + 1] 小于前一个组件的位置时
        if (
          this.currentIndex !== i &&
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;

          // 获取导航里点击的数值 将该数值赋值进入
          this.$refs.nav.currentIndex = this.currentIndex;

          // console.log(this.currentIndex);
        }
      }
    },

    // 导航点击判断位置
    titleClick(index) {
      this.$refs.scroll.scroll.scrollTo(0, -this.themeTopYs[index], 0);
    },

    // 监听商品图片加载完毕获取各组件的位置
    imageLoad() {
      this.$refs.scroll.scroll.refresh();

      // 调用获取组件位置
      this.getThemeTopY();
    },

    // 购买加入购物车并放入本地存储
    addToCart() {
      // 拿到进入购物车的数据
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      console.log(product);

      // $store是vuex
      // commit 是 mutations
      // dispatch 是 actions
      this.$store.dispatch("addCart", product);

      // 将购物车信息转换为字符串放入本地存储
      window.localStorage.setItem(
        "cartList",
        JSON.stringify(this.$store.state.cartList)
      );

      // 收藏弹出
      this.isBuyShow = true;
      this.$store.state.cartList.forEach((item) => {
        // console.log(item);
        if (item.count > 1) {
          this.buyOrCollect = "已收藏,数量+1";
        } else {
          this.buyOrCollect = "收藏成功";
        }
      });
      this.buyPrice = "";

      setTimeout(() => {
        this.isBuyShow = false;
      }, 1000);
    },

    // 点击弹出已收藏

    // 购买弹出
    buyClick() {
      this.isBuyShow = true;
      this.buyOrCollect = "购买成功";
      this.buyPrice = this.goods.realPrice + "块";

      setTimeout(() => {
        this.isBuyShow = false;
      }, 1000);
    },
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  height: 100vh;
  background-color: #fff;
  z-index: 9;
  overflow: hidden;
}

.scroll {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  height: calc(100% - 93px);
  /* background-color: #f9f9f6; */
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #f9f9f6;
}
</style>
